package com.seveneight.baijiesteward.ui.main.fragment;

import android.os.Bundle;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.base.BaseFragment;
import com.seveneight.baijiesteward.base.MessageEvent;
import com.seveneight.baijiesteward.ui.main.presenter.LoginPresenter;
import com.seveneight.baijiesteward.ui.user.fragment.ManagerAccountFragment;
import com.seveneight.baijiesteward.utils.TabSelectedEvent;

import me.yokeyword.eventbusactivityscope.EventBusActivityScope;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by user1 on 2018/2/6.
 */

public class MainFragment extends BaseFragment {
    public static final int FIRST = 0;
    public static final int SECOND = 1;
    public static final int THIRD = 2;
    private FrameLayout layout_content;
    private SupportFragment[] mFragments = new SupportFragment[4];
    private BottomNavigationBar bottombar;

    @Override
    protected int getContentView() {
        return R.layout.fragment_main;
    }

    public static MainFragment newInstance() {
        Bundle args = new Bundle();
        MainFragment fragment = new MainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {

        SupportFragment firstFragment = findChildFragment(FirstFragment.class);
        if (firstFragment == null) {
            mFragments[0] = FirstFragment.newInstance();
            mFragments[1] = CreditFragment.newInstance();
            mFragments[2] = ShareFragment.newInstance();
            mFragments[3] = UserFragment.newInstance();
            loadMultipleRootFragment(R.id.layout_content, 0,
                    mFragments[0],
                    mFragments[1],
                    mFragments[2],
                    mFragments[3]);
        } else {
            mFragments[0] = FirstFragment.newInstance();
            mFragments[1] = CreditFragment.newInstance();
            mFragments[2] = ShareFragment.newInstance();
            mFragments[3] = UserFragment.newInstance();
        }
        bottombar = findView(R.id.bottombar);
        bottombar.setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

        bottombar //值得一提，模式跟背景的设置都要在添加tab前面，不然不会有效果。
                .setActiveColor(R.color.mainblue)//选中颜色 图标和文字
                .setInActiveColor(R.color.tab_unselect)//默认未选择颜色
                .setBarBackgroundColor(R.color.white);//默认背景色
        bottombar.
                addItem(new BottomNavigationItem(R.mipmap.icon_first_check, "首页")).
                addItem(new BottomNavigationItem(R.mipmap.icon_credit, "戒律")).
                addItem(new BottomNavigationItem(R.mipmap.share_img, "分享")).
                addItem(new BottomNavigationItem(R.mipmap.icon_my, "我的")).
                setFirstSelectedPosition(0).
                initialise();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {
        bottombar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {

            @Override
            public void onTabSelected(int position) {
                if (userBean!=null){
//                    if (userBean.getAuthStatus().equals("-1") && (position==0 || position==3)){
                    new LoginPresenter(null).getUserInfo("",getContext(),jsonFormat.commonParam());
//                    }
                }
                showHideFragment(mFragments[position]);
            }

            @Override
            public void onTabUnselected(int position) {

            }

            @Override
            public void onTabReselected(int position) {
                EventBusActivityScope.getDefault(_mActivity).post(new TabSelectedEvent(position));
            }
        });
    }

    /**
     * start other BrotherFragment
     */
    public void startBrotherFragment(SupportFragment targetFragment) {
        start(targetFragment);
    }


}
